package com.example.birdfriends.utils.mail;

import cn.hutool.core.io.FileTypeUtil;
import cn.hutool.core.io.FileUtil;

import java.io.*;
import java.util.*;

public class Test {
    private static final String filepath = "C:\\Users\\Administrator\\IdeaProjects\\BirdFriends\\src\\main\\java\\com\\example\\birdfriends\\utils\\files/test.txt";

    public static void main(String[] args) throws IOException {
        String dir = "D:\\bf-files\\springboot-upload\\bird-images\\";
        String birdname = "哈哈";
        String path = dir + birdname;
        deleteFolder(path);
    }

    public static void deleteFolder(String path){
        File file = new File(path);

        File[] fileList = file.listFiles();   // 获取当前文件夹下的所有文件
        for (File f: fileList){
            if(f.isDirectory()){
                deleteFolder(f.getPath());
                if (f.delete()){
                    System.out.println("删除文件成功："+f.getPath());
                }else {
                    System.out.println("删除文件失败："+f.getPath());
                }
            }else {
                if (f.delete()){
                    System.out.println("删除文件成功："+f.getPath());
                }else {
                    System.out.println("删除文件失败："+f.getPath());
                }
            }
        }

        if (file.delete()){
            System.out.println("删除文件成功："+file.getPath());
        }else {
            System.out.println("删除文件失败："+file.getPath());
        }
    }

    public static void writeFile(File file, String sequence){
        OutputStream out = null;
        try{
            if(!file.exists()){
                file.createNewFile();  // 如果文件不存在就创建一个新文件
                out = new FileOutputStream(file);
            }else {
                out = new FileOutputStream(file, true);
            }

            OutputStreamWriter writer = new OutputStreamWriter(out, "UTF-8");
            writer.append(sequence);
            writer.append("\r\n");
            writer.close();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static List<String> readFile(File file) throws IOException {
        InputStream in = new FileInputStream(file);
        InputStreamReader reader = new InputStreamReader(in, "UTF-8");

        StringBuffer sb = new StringBuffer();

        while (reader.ready()){
            sb.append((char) reader.read());
        }
        String seqs = sb.toString();
        String[] strs = seqs.split("\r\n");

        List<String> sequenceList = new ArrayList<>(Arrays.asList(strs));
        System.out.println(sequenceList.toString());

        reader.close();
        in.close();
        return sequenceList;
    }

    public static String getRandomStr(){
        final String strs = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        final int strLength = 8;
        for(int i = 0; i < strLength; i++){
            sb.append(strs.charAt(random.nextInt(strs.length())));
        }

        return sb.toString();
    }
}
